﻿<!--#INCLUDE FILE="config.asp"-->
<!--#include virtual ="/Email/email-costabaja.asp"-->
<%
if(Session("AdminAccessLevel") <> 1 and Session("AdminAccessLevel") <> 3) then
	Response.Redirect("admin-login.asp?msg=invalid")
end if

if (request("emailto") <> "") then 
	emailto = request("emailto")
	subject = request("subject")
	content = request("content")
	guestid = request("guestid")
	nameGuest = request("nameGuest")
end if

		Dim FEiEmail
	
		FEiEmail="discover@costabaja.com"
		
		'send mail
		
		From = "discover@costabaja.com"
		FEiEmail = FEiEmail & ";" & emailto

	attach = request("attach")
	attach = Left(attach, Len(attach) - 1)
	if (attach <> "") then
		Dim nameOfFile
		nameOfFile = ""
		
		' Split the string at the comma characters and add each field to a ListBox
		fields = Split(attach, ";")

		For i = 0 To UBound(fields)
			nameOfFile = nameOfFile&""&Server.MapPath("Pdf/"&Trim(fields(i)))&";"
		Next
	
	end if
	nameOfFile = Left(nameOfFile, Len(nameOfFile) - 1) 
	'output = nameOfFile
	actionType = request("type")
	prefix = request("letterPrefix")

	' Read text file
	Dim filename, strFileText
	filename = ""&prefix&"-reservation-detail-letter.txt"
	on error resume next
	Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(server.MapPath("Letter/"&filename),1)
	strFileText = objFileToRead.ReadAll()
	objFileToRead.Close
	Set objFileToRead = Nothing
	
	' if prefix not match with filename into server => don't create pdf
	if (strFileText <> "") then
	
	'============================='
	'	Create PDF				  '
	'============================='
	Set Pdf = Server.CreateObject("Persits.Pdf")
	Set Doc = Pdf.CreateDocument

	Doc.Title = "Reservation Detail Letter"
	Doc.Creator = "Costabaja Admin"

	Set Page = Doc.Pages.Add

	Set Font = Doc.Fonts("Helvetica")

	Params = "x=0; y=650; width=612; alignment=center; size=14"

	Page.Canvas.DrawText ""&strFileText, Params, Font

	'============================='
	'	End create PDF				  '
	'============================='
	
		' save to system
		filename1 = ""&nameGuest&"_"&Year(Now())&"_"&Month(Now())&"_"&Day(Now())&"_Costabaja-ReservationDetails.pdf"
		path = Doc.Save(Server.MapPath("Pdf/"&filename1), False )
		
		Const OverwriteExisting = True
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		objFSO.CopyFile ""&Server.MapPath("Pdf/"&filename1) , ""&Server.MapPath("Costabaja-ReservationDetails.pdf"), OverwriteExisting
	
		
		'On Error Resume Next 
			Call EmailCdonts(From,FEiEmail,"",subject,content ,0,""&Server.MapPath("Costabaja-ReservationDetails.pdf")&";"&nameOfFile,2)
			
		 Set obj = CreateObject("Scripting.FileSystemObject") 'Calls the File System Object
		 obj.DeleteFile(Server.MapPath("Costabaja-ReservationDetails.pdf")) 'Deletes the file throught the DeleteFile function
	else 
			'On Error Resume Next 
			Call EmailCdonts(From,FEiEmail,"",subject,content ,0,""&nameOfFile,2)
	end if
	
		  If Err.Number <> 0 Then
		 Dim output
		   output = "This means there is most likely a problem with the" & vbCrLf
		   output = output + """ConnectionString"" info that you specified.<br>" & vbCrLf
		  end if
		' Else
			' set objConnect = server.createObject("ADODB.Connection")       
			' objConnect.open conStr

			' Set oCmd = Server.CreateObject("ADODB.Command")

			' oCmd.ActiveConnection = objConnect
			' oCmd.CommandText = "sp_sendmail_pdf_guest" 
			' oCmd.CommandType = 4
			
			' Set guestid = oCmd.CreateParameter("@guestid", 3,1,100, guestid)
			' oCmd.Parameters.Append guestid
			
			' Set userid = oCmd.CreateParameter("@userid", 3,1,100,session("AdminId"))
			' oCmd.Parameters.Append userid

			' set rss = oCmd.execute
			
			' Response.Write("Send Successfully! Click <a href=""admin-dashboard.asp"">here</a> to redirect Dashboard")
		' end if

	Response.Write output
%>